import { getAdminInfo } from '@/api/index'
import VueCookies from 'vue-cookies'
import Vue from 'vue'
import router from '@/router'
Vue.use(VueCookies)
export default {
  namespaced: true,
  state: {
    adminName: '',
    avatar: '',
    admin_id: null
  },
  mutations: {
    getAdminInfo (state, layload) {
      state.adminName = layload.name
      state.avatar = layload.avatar
      state.admin_id = layload.admin_id
    },
    logout (state) {
      state.adminName = '' // 清除用户信息
      state.avatar = '' // 清除用户头像
      VueCookies.remove('admin_id', { path: '' }) // 清除cookie
      router.push('/login') // 返回登录页面
    }
  },
  actions: {
    // 获取管理员信息
    async getAdminInfo ({ commit }, adminId) {
      const res = await getAdminInfo(adminId)
      commit('getAdminInfo', res.data)
    },
    // 退出
    logout ({ commit }) {
      commit('logout')
    }
  }
}
